package com.example.testapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class MyToggleButton extends View implements View.OnClickListener {
    private Bitmap backgroundBitMap;
    private Bitmap slidingBitMap;
    private Paint paint;
    /**
     * 距离左边最大距离
     */
    private int slidLeftMax;
    private int slideLeft;
    /**
     * true:点击事件生效，滑动事件不生效
     * false:点击事件不生效，滑动事件生效
     */
    private boolean isEnableClick = true;
    private boolean isOpen = false;

    private float startX;
    private float lastX;

    // 如果我们在布局文件使用该类，将会用这个构造方法实例该类，如果没有就崩溃
    public MyToggleButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }
    // 初始化
    private void initView(){
        paint = new Paint();
        paint.setAntiAlias(true); // 设置抗锯齿
        backgroundBitMap = BitmapFactory.decodeResource(getResources(),R.drawable.switch_background);
        slidingBitMap = BitmapFactory.decodeResource(getResources(),R.drawable.slide_button);
        slidLeftMax = backgroundBitMap.getWidth() - slidingBitMap.getWidth();
        setOnClickListener(this);


    }

    // 视图的测量

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(backgroundBitMap.getWidth(),backgroundBitMap.getHeight());
    }

    // 绘制


    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        canvas.drawBitmap(backgroundBitMap,0,0,paint);
        canvas.drawBitmap(slidingBitMap,slideLeft,0,paint);

    }

    @Override
    public void onClick(View view) {
        if(isEnableClick){
            isOpen = !isOpen;
            flushView();
        }

    }

    private void flushView() {
        if(isOpen){
            slideLeft = slidLeftMax;
        }else{
            slideLeft = 0;
        }
        invalidate(); //会导致onDraw()执行
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);  // 执行父类的方法
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                // 1.记录按下的坐标
                lastX = startX = event.getX();
                isEnableClick = true;
                break;
            case MotionEvent.ACTION_MOVE:
                //2.计算结束值
                float endX = event.getX();
                //3.计算偏移量
                float distanceX = endX - startX;

//                slideLeft = (int) (slideLeft + distanceX);
                slideLeft += distanceX;
                if(slideLeft <0){
                    slideLeft = 0;
                }else if(slideLeft>slidLeftMax){
                    slideLeft = slidLeftMax;
                }
                //4.屏蔽非法值
                //5.刷新
                invalidate();

                //6.数据还原
                startX = event.getX();

                if(Math.abs(endX - lastX) > 5){
                    //滑动
                    isEnableClick = false;
                }



                break;
            case MotionEvent.ACTION_UP:

                if(!isEnableClick){
                    if(slideLeft > slidLeftMax/2){

                        //显示按钮开
                        isOpen = true;
                    }else{

                        isOpen = false;

                    }
                    flushView();
                }



                break;

        }
        return true;
    }
}
